-- This function saves the user data
function saveData()
	local playeraccount = getPlayerAccount (source)
	if (playeraccount) then
		local playerx, playery, playerz = getElementPosition (source)
		local playermoney = getPlayerMoney (source)
		local playerteam = getPlayerTeam (source)
		setAccountData (playeraccount, "rpg.newplayer", "false")
		setAccountData (playeraccount, "rpg.money", playermoney)
		setAccountData (playeraccount, "rpg.playerx", playerx)
		setAccountData (playeraccount, "rpg.playery", playery)
		setAccountData (playeraccount, "rpg.playerz", playerz)
		setAccountData (playeraccount, "rpg.playerteam", playerteam)
	end
end

-- This function loads the user data
function loadData()
	local playeraccount = getPlayerAccount(source)
	local teamHobos = getTeamFromName("hobos")
	if (playeraccount) then
		local newplayer = getAccountData (playeraccount, "rpg.newplayer")
		if (newplayer) then
			local playermoney = getAccountData (playeraccount, "rpg.money")
			local playerx = getAccountData (playeraccount, "rpg.playerx")
			local playery = getAccountData (playeraccount, "rpg.playery")
			local playerz = getAccountData (playeraccount, "rpg.playerz")
			local playerteam = getAccountData (playeraccount, "rpg.playerteam")
			setPlayerMoney (source, playermoney)
			spawnPlayer(source, playerx, playery, playerz)
			fadeCamera(source, true)
			setCameraTarget(source, source)
			setPlayerTeam(source, playerteam)
		else
			outputChatBox("Welcome, new player!", source)
			setPlayerTeam(source, teamHobos)
		end
	end
end

-- This function creates the teams
function createTeams ()
	local teamCops = createTeam ("cops", 0, 0, 255)
	local teamMedics = createTeam ("medics", 0, 255, 255)
	local teamCriminal = createTeam ("criminals", 250, 10, 10)
	local teamHobos = createTeam("hobos", 9, 10, 10)
end

local vehicles = getElementsByType ("vehicle")

function fuelTankExplosion(theVehicle)
	for theKey, theVehicle in pairs(vehicles) do
		if ( isVehicleFuelTankExplodable (Vehicle) == false ) then
			setVehicleFuelTankExplodable ( Vehicle, true )
		end
	end
end

addEventHandler("onPlayerQuit", getRootElement(), saveData)
addEventHandler("onPlayerLogin", getRootElement(), loadData)
addEventHandler("onClientResourceStart", getRootElement(), fuelTankExplosion)
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), createTeams)